<?php

namespace app\api\controller\shop;

use app\common\controller\Api;
use think\Db;
use think\db\Query;
use think\Exception;
use think\Config;


/**
 * 门店B端小程序接口
 */
class User extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 构造方法
     * @access public
     * @param Request $request Request 对象
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 门店详情
     * 
     * 
     *  
     */
    public function shopinfo()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        //$shop_id = $this->request->request('shop'); 

        // $where['id'] = $this->shop_id;
        $where['id'] = $this->shop_id;

        $list = Db::name('wanlshop_shop')
            ->where($where)
            ->find();



        $sql = "
        SELECT ifNULL(sum(actual_price),0) as sumpay,count(*) as sumno 
        FROM fa_wanlshop_order
        where shop_id = " . $this->shop_id . "
        and state in (2,3,4,5,6)
        and to_days(FROM_UNIXTIME(createtime)) = to_days(now())
        ";
        $data = db::query($sql);


        //今天
        $list['t_income'] = $data[0]['sumpay'];  //收款
        $list['t_refund'] = 0.00;  //退款
        $list['t_pay'] = $data[0]['sumpay'];   //实际
        $list['t_sum'] = $data[0]['sumno'];   //开单

        $sql = "
        SELECT ifNULL(sum(actual_price),0) as sumpay,count(*) as sumno 
        FROM fa_wanlshop_order
        where shop_id = " . $this->shop_id . "
        and state in (2,3,4,5,6)
        and DATE_FORMAT( FROM_UNIXTIME(createtime), '%Y%m%d' ) = CURDATE() -INTERVAL 1 DAY
        ";
        $data1 = db::query($sql);


        //昨天
        $list['y_income'] = $data1[0]['sumpay'];
        $list['y_refund'] = 0.00;
        $list['y_pay'] = $data1[0]['sumpay'];
        $list['y_sum'] = $data1[0]['sumno']; //开单

        $sql = "
        SELECT ifNULL(sum(actual_price),0) as sumpay,count(*) as sumno 
        FROM fa_wanlshop_order
        where shop_id = " . $this->shop_id . "
        and state in (2,3,4,5,6)
        and DATE_FORMAT( FROM_UNIXTIME(createtime), '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
        ";
        $data2 = db::query($sql);

        //本月
        $list['m_sum'] = $data2[0]['sumno']; //开单
        $list['m_income'] = $data2[0]['sumpay']; //业绩

        
        // $list['address_id'] = 7; //订货地址

        $list['address_id'] = 0;
        $addressinfo = Db::name('wanlshop_shop_address')->field('id,name,mobile,address')->where(['shop_id'=>$this->shop_id,'default'=>1])->find();
        if(!empty($addressinfo)){
            $list['address_id'] = $addressinfo['id']; 
        }

        $list['address_info'] =  $addressinfo; //订货地址

        
        $list['firstloan'] = 5000.00 ;//贷款金额

        $list['cash_1'] = $list['payment'];//可提现金额
        $list['cash_2'] = $list['settled'];//已提现金额
        $list['cash_3'] = $list['accumulate'];//待提现金额

        $list['card_1'] = "楼小魅疼痛调理馆大石店";//账号名称
        $list['card_2'] = "工商银行";//开户银行
        $list['card_3'] = "7301144064217092096";//提现卡账号
        $list['pay_img'] = Config::get('site.corporate_payment');

        $this->success('ok', $list);

    }


    /**
     * 会员列表
     * 
     * 
     *  @param string $keyword  姓名\编号\手机
     */
    public function userlist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
        $shop_id = $this->shop_id;

        if ($keyword) {
            $where['u.nickname|u.code|u.mobile'] = ['like', '%' . $keyword . '%'];
        }
        $where['u.status'] = 'normal';
        $where['u.shop_id'] = $shop_id;
        // $where['user_id'] = $this->auth->id;

        $list = Db::name('user u')
            ->join('staff s', 's.id = u.marketing', 'left')
            ->where($where)
            ->field('u.id,u.group_id,u.shop_id,u.username,u.nickname,u.money,u.email,u.score,u.mobile,u.avatar,u.gender,u.birthday,u.code,u.juser_id,u.remark,u.jointime,u.logintime,u.promoter,s.name')
            ->order('u.updatetime desc')
            ->paginate();

        $this->success('ok', $list);

    }

    /**
     * 门店列表
     * 
     * 
     *  @param string $keyword  店铺名称
     */
    public function shoplist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');

        if ($keyword) {
            $where['shopname'] = ['like', '%' . $keyword . '%'];
        }
        $where['status'] = 'normal';
        // $where['user_id'] = $this->auth->id;

        $list = Db::name('wanlshop_shop')
            ->where($where)
            ->field('id,shopname,city')
            ->order('updatetime desc')
            ->paginate();

        $this->success('ok', $list);

    }

    /**
     * 员工排班
     * 
     * 
     *  @param string $keyword  时间
     */
    public function classes()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
        $time = date("Y-m", $keyword);

        $time = strtotime($time);
        $endtime = strtotime("+1 month", $keyword);
        $endtime = date("Y-m", $endtime);

        $endtime = strtotime($endtime);

        if ($keyword) {
            $where['scheduletime'] = ['between time', [$time, $endtime]];
        }
        $where['s.status'] = 'normal';
        $where['s.staff_id'] = $this->auth->staff_id;

        $list = Db::name('staff_class s')
            ->join('classes c', 's.classes_id = c.id', 'left')
            ->where($where)
            ->field('s.id,s.starttime,s.endtime,s.scheduletime,c.code,c.name')
            ->order('s.updatetime desc')
            ->paginate();

        $this->success('ok', $list);

    }

    /**
     * 员工记录
     * 
     * 
     *  @param string $keyword  时间
     */
    public function clocklist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
        $time = date("Y-m", $keyword);

        $time = strtotime($time);
        $endtime = strtotime("+1 month", $keyword);
        $endtime = date("Y-m", $endtime);

        $endtime = strtotime($endtime);

        if ($keyword) {
            $where['clocktime'] = ['between time', [$time, $endtime]];
        }

        $where['staff_id'] = $this->auth->staff_id;

        $list = Db::name('staff_clock')
            ->where($where)
            //->field('s.id,s.starttime,s.endtime,s.scheduletime,c.code,c.name')
            ->order('clocktime desc')
            ->paginate();

        $this->success('ok', $list);

    }

    /**
     * 员工当天情况记录
     * 
     * 
     *  @param string $keyword  时间
     */
    public function clockday()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
      

        if ($keyword) {
            $time = date("Y-m-d", $keyword);

            $time = strtotime($time);
            $endtime = strtotime("+1 day", $keyword);
            $endtime = date("Y-m-d", $endtime);
    
            $endtime = strtotime($endtime);
            $where['clocktime'] = ['between time', [$time, $endtime]];
        }

        $where['staff_id'] = $this->auth->staff_id;

        $list['clock_0'] = Db::name('staff_clock')
            ->where($where)
            ->where('type', '=', 0)
            //->field('s.id,s.starttime,s.endtime,s.scheduletime,c.code,c.name')
            ->order('clocktime')
            ->find();

        $list['clock_1'] = Db::name('staff_clock')
            ->where($where)
            ->where('type', '=', 1)
            //->field('s.id,s.starttime,s.endtime,s.scheduletime,c.code,c.name')
            ->order('clocktime desc')
            ->find();

        $this->success('ok', $list);

    }


    /**
     * 员工打卡
     * 
     * 
     *  @param int $clocktime  时间
     *  @param int $type  类型
     *  @param string $status  类别
     */
    public function clock()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $clocktime = $this->request->request('clocktime');
        $type = $this->request->request('type');
        $status = $this->request->request('status');

        $date = [
            'staff_id' => $this->auth->staff_id,
            'clocktime' => $clocktime,
            'status' => $status,
            'type' => $type,
            'createtime' => time()

        ];

        $list = Db::name('staff_clock')->insert($date);

        $this->success('ok', $list);

    }


    /**
     * 订单列表 -商品/服务
     * 
     * @param string $gdtype  0=商品,1=服务
     * @param string $state
     */
    public function orderlist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $gdtype = $this->request->request('gdtype');
        $state = $this->request->request('state');
        // $time = date("Y-m", $keyword);

        // $time =  strtotime($time);
        // $endtime = strtotime("+1 month", $keyword);
        // $endtime = date("Y-m", $endtime);

        // $endtime = strtotime($endtime);

        //  if ($keyword) {
        //      $where['clocktime'] = ['between time', [$time, $endtime]];      
        //  }

        $where['o.shop_id'] = $this->shop_id;
        $where['s.gdtype'] = $gdtype;
        if ($state) {
            $where['o.state'] = $state;
        }
        $list = Db::name('wanlshop_order o')
            ->join('wanlshop_order_goods s', 's.order_id = o.id', 'inner')
            ->where($where)
            ->field('o.id,s.title,s.image,difference,s.actual_payment,s.number,o.order_no,o.actual_price,o.state,s.gdtype')
            ->order('s.id desc')
            ->paginate();

        $this->success('ok', $list);

    }

    /**
     * 财务概况 结算明细
     * 
     * 
     * @param string $status  状态
     */
    public function financelist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        $status = $this->request->request('status');
        // $time = date("Y-m", $keyword);

        // $time =  strtotime($time);
        // $endtime = strtotime("+1 month", $keyword);
        // $endtime = date("Y-m", $endtime);

        // $endtime = strtotime($endtime);

        //  if ($keyword) {
        //      $where['clocktime'] = ['between time', [$time, $endtime]];      
        //  }

        $where['e.shop_id'] = $this->shop_id;

        if ($status) {
            $where['e.status'] = $status;
        }
        $list = Db::name('shop_entry e')
            ->join('wanlshop_order o', 'e.order_id = o.id', 'left')
            ->where($where)
            ->field('o.id,o.order_no,o.ontype,e.amount,e.status,e.createtime')
            ->order('e.id desc')
            ->paginate();

        $this->success('ok', $list);

    }

    /**
     * 提现记录
     * 
     * 
     *  @param string $keyword  时间
     */
    public function cashlist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
     

        if ($keyword) {
            $time = date("Y-m", $keyword);

            $time = strtotime($time);
            $endtime = strtotime("+1 month", $keyword);
            $endtime = date("Y-m", $endtime);
    
            $endtime = strtotime($endtime);
            $where['createtime'] = ['between time', [$time, $endtime]];
        }

        $where['shop_id'] = $this->shop_id;

        $list = Db::name('lal_member_withdraw')
            ->where($where)
            //->field('s.id,s.starttime,s.endtime,s.scheduletime,c.code,c.name')
            ->order('createtime desc')
            ->paginate();

        $this->success('ok', $list);

    }

    /**
     * 申请提现
     * 
     *  @param string $keyword  提现金额
     *  
     *  
     */
    public function withdraw()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
        $shop = $this->shop_id;

        $date = [
            'code' => time(),
            'type' => 0,
            'shop_id' => $shop,
            'name' => '',
            'amount' => $keyword,
            'createtime' => time()

        ];

        $list = Db::name('lal_member_withdraw')->insert($date);

        $this->success('ok', $list);


    }

    /**
     * 经营数据
     * 
     * 
     *  @param string $keyword  时间
     */
    // public function operateday()
    // {
    //     //设置过滤方法
    //     $this->request->filter(['strip_tags']);
    //     $keyword = $this->request->request('keyword');

    //     $where = "";
    //     if ($keyword) {
    //         $time = date("Y-m-d", $keyword);

    //         $time = strtotime($time);
    //         $endtime = strtotime("+1 day", $keyword);
    //         $endtime = date("Y-m-d", $endtime);

    //         $endtime = strtotime($endtime);
    //         $where = " and o.createtime >= " . $time . "
    //         and o.createtime < " . $endtime;
    //     }


    //     $list = [];

    //     $sql = "
    //     SELECT ifNULL(sum(actual_price),0) as sumpay
    //     FROM fa_wanlshop_order as o
    //     where shop_id = " . $this->shop_id . "
    //     and state in (2,3,4,5,6)
    //     " . $where
    //     ;
    //     $data1 = db::query($sql);

    //     $list['data_1'] = $data1[0]['sumpay']; // 实际收款


    //     $list['data_2'] = 0.00; // 消耗金额

    //     $sql = "
    //     SELECT ifNULL(sum(i.actual_price),0) as sumpay
    //     FROM fa_wanlshop_order_item as i
    //     left join fa_wanlshop_order as o on o.id = i.order_id
    //     where o.shop_id = " . $this->shop_id . "
    //     and o.state in (2,3,4,5,6)
    //     " . $where
    //     ;
    //     $data3 = db::query($sql);

    //     $list['data_3'] = $data3[0]['sumpay']; // 开卡金额

    //     $sql = "
    //    SELECT ifNULL(sum(i.actual_price),0) as sumpay
    //    FROM fa_wanlshop_order_item as i
    //    left join fa_wanlshop_order as o on o.id = i.order_id
    //    where o.shop_id = " . $this->shop_id . "
    //    and o.state in (2,3,4,5,6)
    //    and i.type = 4
    //    " . $where
    //     ;
    //     $data4 = db::query($sql);

    //     $list['data_4'] = $data4[0]['sumpay']; // 充值收入

    //     $sql = "
    //    SELECT ifNULL(sum(i.actual_payment),0) as sumpay
    //    FROM fa_wanlshop_order_goods as i
    //    left join fa_wanlshop_order as o on o.id = i.order_id
    //    where o.shop_id = " . $this->shop_id . "
    //    and o.state in (2,3,4,5,6)
    //    and i.gdtype = 1
    //    " . $where
    //     ;
    //     $data5 = db::query($sql);

    //     $list['data_5'] = $data5[0]['sumpay']; // 服务收入
    //     $sql = "
    //    SELECT ifNULL(sum(i.actual_payment),0) as sumpay
    //    FROM fa_wanlshop_order_goods as i
    //    left join fa_wanlshop_order as o on o.id = i.order_id
    //    where o.shop_id = " . $this->shop_id . "
    //    and o.state in (2,3,4,5,6)
    //    and i.gdtype = 0
    //    " . $where
    //     ;
    //     $data6 = db::query($sql);

    //     $list['data_6'] = $data6[0]['sumpay']; // 产品收入

    //     $list['data_7'] = 0.00; // 退款

    //     $this->success('ok', $list);

    // }

    public function operateday()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $starttime = $this->request->request('starttime');
        $endtime = $this->request->request('endtime');
        $shop_id = $this->request->request('shop_id');

        /*** 总的实际收款 ***/
        $where = [];
        if ($starttime && $endtime) {
            $where['createtime'][] = ['>=',$starttime];
            $where['createtime'][] = ['<=',$endtime];
        }

        if ($shop_id != 1) {
            $where['shop_id'] = $shop_id;
        }

        //$where['state'] = ['in',[2,3,4,5,6]];
        $where['state'] = 6;

        $list = [];
        $result = Db::name('wanlshop_order')
            ->where($where)
            ->whereNull('deletetime')
            ->sum('actual_price');
        //echo Db::name('wanlshop_order')->getLastSql();exit;
        $list['data_1'] = is_null($result)? "0.00" : sprintf('%.2f',$result);

        $where = [];
        if ($starttime && $endtime) {
            $where['o.createtime'][] = ['>=',$starttime];
            $where['o.createtime'][] = ['<=',$endtime];
        }

        if ($shop_id != 1) {
            $where['o.shop_id'] = $shop_id;
        }

        $where['o.state'] = '6';
        //$where['o.state'] = ['in',[2,3,4,5,6]];
        //$where['o.data_type'] =['in',['cards','goods']];
        $where['p.type'] = '2';

        /*$result = Db::name('lal_member_entry')->alias('l')
            ->join('wanlshop_order o','o.id=l.order_id','left')
            ->join('wanlshop_pay_out_trade t', 'o.id=t.order_id', 'left')
            ->join('pay p', 'p.id=t.pay_way_id', 'left')
            ->where($where)
            ->sum('l.bbalance');*/
        $result = Db::name('wanlshop_order')->alias('o')
                  ->join('wanlshop_pay_out_trade t', 'o.id=t.order_id', 'left')
                  ->join('pay p', 'p.id=t.pay_way_id', 'left')
                  ->where($where)
                  //->sum('actual_price');
                  ->sum('t.price');
        //echo Db::name('lal_member_entry')->getLastSql();exit;
        $list['data_8'] = is_null($result)? "0.00" : sprintf('%.2f',$result);


        /** 卡项消耗 **/

        $where2=[];
        $where2['o.data_type'] = ['<>','merchant'];
        if ($shop_id != 1) {
            $where2['o.shop_id'] = $shop_id;
        }
        $data_2 = Db::name('wanlshop_order')->alias('o')
            ->join('wanlshop_order_item_pay p', 'o.id=p.order_id', 'left')
            ->where('o.createtime', 'between time', [$starttime, $endtime])
            ->where($where2)
            ->where('o.state', 6)
            ->sum('p.money');
        $list['data_2'] =$data_2 ?? 0.00;
        /** 实际付款收入 **/
        $where = [];
        if ($starttime && $endtime) {
            $where['o.createtime'][] = ['>=',$starttime];
            $where['o.createtime'][] = ['<=',$endtime];
        }

        if ($shop_id != 1) {
            $where['o.shop_id'] = $shop_id;
        }

        $where['o.state'] = ['in',[2,3,4,5,6]];

        $sumpay = Db::name('wanlshop_order_item i')
            ->join('wanlshop_order o', 'o.id = i.order_id', 'LEFT')
            ->where($where)
            ->where(['i.type'=>['in',[1,2]]])
            ->sum('i.actual_price');
        //echo Db::name('wanlshop_order_item i')->getLastSql();exit;
        $list['data_3'] = is_null($sumpay)? 0.00 : sprintf('%.2f',$sumpay);


        /** 充值收入 **/
        //$where['i.type'] = 4;
        $sumpay = Db::name('wanlshop_order_item i')
            ->join('wanlshop_order o', 'o.id = i.order_id', 'LEFT')
            ->where($where)
            ->where(['i.type'=>['in',[4]]])
            ->sum('i.actual_price');

        $list['data_4'] = is_null($sumpay)? 0 : sprintf('%.2f',$sumpay);


        $where = [];
        if ($starttime && $endtime) {
            $where['o.createtime'][] = ['>=',$starttime];
            $where['o.createtime'][] = ['<=',$endtime];
        }
        if ($shop_id != 1) {
            $where['o.shop_id'] = $shop_id;
        }
        $where['o.state'] = ['in',[2,3,4,5,6]];

        $where['g.gdtype'] = '1';
        $sumpay = Db::name('wanlshop_order_goods g')
            ->join('wanlshop_order o', 'o.id = g.order_id', 'LEFT')
            ->where($where)
            ->sum('o.actual_price');
        $where2 = [];
        $where2['o.data_type'] = ['<>','merchant'];
        if ($shop_id != 1) {
            $where2['o.shop_id'] = $shop_id;
        }
        $sumpay = Db::name('wanlshop_order_goods')->alias('i')
                ->join('wanlshop_order o', 'o.id=i.order_id', 'left')
                ->join('wanlshop_pay_out_trade t', 'o.id=t.order_id', 'left')
                ->join('pay p', 'p.id=t.pay_way_id', 'left')
                ->where($where2)
                ->where('p.type','<>','1')
                ->where('o.createtime', 'between time', [$starttime, $endtime])
                ->field('sum(i.number * i.actual_payment ) as sum')
                ->where('o.state',  '6')
                ->where('i.gdtype',  '1')
                ->find();
        $list['data_5'] = isset($sumpay['sum'])? sprintf('%.2f',$sumpay['sum']) : '0.00';

        /** 商品收入 **/
//        $where['g.gdtype'] = '0';
//        $sumpay = Db::name('wanlshop_order_goods g')
//            ->join('wanlshop_order o', 'o.id = g.order_id', 'LEFT')
//            ->where($where)
//            ->sum('o.actual_price');
        $where = [];
        $where['o.data_type'] = ['<>','merchant'];
        if ($shop_id != 1) {
            $where['o.shop_id'] = $shop_id;
        }
        $data_6 = Db::name('wanlshop_order_goods')->alias('i')
            ->join('wanlshop_order o', 'o.id=i.order_id', 'left')
            ->join('wanlshop_pay_out_trade t', 'o.id=t.order_id', 'left')
            ->join('pay p', 'p.id=t.pay_way_id', 'left')
            ->where($where)
            ->where('p.type','<>','1')
            ->where('o.createtime', 'between time', [$starttime, $endtime])
            ->field('sum(i.number * i.actual_payment ) as sum')
            ->where('o.state', '6')
            ->where('i.gdtype', '0')
            ->select();

        $list['data_6'] = isset($data_6['0']['sum']) ? $data_6['0']['sum'] : sprintf('%.2f',0);
        $where = [];
        if ($starttime && $endtime) {
            $where['createtime'][] = ['>=',$starttime];
            $where['createtime'][] = ['<=',$endtime];
        }
        if ($shop_id != 1) {
            $where['shop_id'] = $shop_id;
        }
        $where['state'] = ['in',[11]];
        $refund =Db::name('wanlshop_order')
            ->where($where)
            ->sum('actual_price');

        $list['data_7'] = $refund ?? 0.00; // 退款
        $list['data_7'] = sprintf('%.2f',$list['data_7']);
        $this->success('ok', $list);

    }

    public function shopsort(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $starttime = $this->request->request('starttime',0);
        $endtime = $this->request->request('endtime',time());
        $shop_id = $this->request->request('shop_id');
        $page = $this->request->request('page',1);
        $page_size = $this->request->request('page_size',15);
        $tab = $this->request->request('tab','0');

        /*** 总的实际收款 ***/
        $where = [];
        if ($starttime && $endtime) {
            $where['o.createtime'][] = ['>=',$starttime];
            $where['o.createtime'][] = ['<=',$endtime];
        }


        $where['o.state'] = ['in',[2,3,4,5,6]];
        $where['o.data_type']=['in',['goods','cards']];
        $list = ['list'=>[],'total'=>0];
        $result=[];
        $count=0;
        $sql="";
        switch ($tab){
            case '0'://实际收款金额
//               $sql =  Db::name('wanlshop_order o')
//                    ->where($where)->whereNull('o.deletetime')->field(["sum(o.actual_price) as price,o.shop_id"])
//                    ->group("o.shop_id")->buildSql();

                $where['o.state'] = '6';
                //$where['o.data_type'] =['in',['cards','goods']];
                $where['p.type'] = '2';

                /*$sql = Db::name('lal_member_entry')->alias('l')
                    ->join('wanlshop_order o','o.id=l.order_id','left')
                    ->join('wanlshop_pay_out_trade t', 'o.id=t.order_id', 'left')
                    ->join('pay p', 'p.id=t.pay_way_id', 'left')
                    ->where($where)
                    ->field(["sum(l.bbalance) as price,o.shop_id"])
                    ->group("o.shop_id")->buildSql();*/
                $sql = Db::name('wanlshop_order')->alias('o')
                       ->join('wanlshop_pay_out_trade t', 'o.id=t.order_id', 'left')
                       ->join('pay p', 'p.id=t.pay_way_id', 'left')
                       ->where($where)
                       ->field(["sum(o.actual_price) as price,o.shop_id"])
                       ->group("o.shop_id")->buildSql();


                break;
            case '1'://客户消耗金额

                $where2=[];
                $where2['o.data_type'] = ['<>','merchant'];
                $data_2 = Db::name('wanlshop_order')->alias('o')
                    ->join('wanlshop_order_item_pay p', 'o.id=p.order_id', 'left')
                    ->where('o.createtime', 'between time', [$starttime, $endtime])
                    ->where($where2)
                    ->where('o.state', 6)
                    ->sum('p.money');

                //$_where = ['p.card_type'=>4];
                $where['o.data_type'] = ['<>','merchant'];
                $where['o.state']=6;
                $sql =Db::name('wanlshop_order_item_pay p')
                    ->join('wanlshop_order o','p.order_id=o.id')
                    ->where($where)
                    ->field(["sum(p.money) as price","o.shop_id"])
                    ->group("o.shop_id")
                    ->buildSql();


                break;
            case '2'://开卡金额
                $where['i.type']=['in',[1,2,3]];
                $sql = Db::name('wanlshop_order o')
                    ->join('wanlshop_order_item i','o.id=i.order_id')
                    ->where($where)
                    ->field(["sum(i.actual_price) as price","o.shop_id"])
                    ->group("o.shop_id")
                    ->buildSql();
                break;
            case '3'://充值金额
                $where['i.type']=4;
                $sql = Db::name('wanlshop_order o')
                    ->join('wanlshop_order_item i','o.id=i.order_id')
                    ->where($where)
                    ->field(["sum(i.actual_price) as price","o.shop_id"])
                    ->group("o.shop_id")
                    ->buildSql();
                break;
            case '4'://服务收入
                //$where['g.gdtype']='1';

//                $sql = Db::name('wanlshop_order o')
//                ->join('wanlshop_order_goods g','o.id=g.order_id')
//                ->where($where)
//                ->field(["sum(o.actual_price) as price","o.shop_id"])
//                ->group("o.shop_id")
//                ->buildSql();
                $where['o.state']='6';
                $sql = Db::name('wanlshop_order_goods')->alias('g')
                    ->join('wanlshop_order o', 'o.id=g.order_id', 'left')
                    ->join('wanlshop_pay_out_trade t', 'o.id=t.order_id', 'left')
                    ->join('pay p', 'p.id=t.pay_way_id', 'left')
                    ->where($where)
                    ->where('p.type','<>','1')
                    //->where('o.createtime', 'between time', [$starttime, $endtime])
                    ->field(['sum(g.number * g.actual_payment ) as price','o.shop_id'])
                    //->where('o.state',  '6')
                    ->where('g.gdtype',  '1')
                    ->group("o.shop_id")
                    ->buildSql();
                break;
            case '5'://产品收入
                //$where['g.gdtype']='0';
                $sql = Db::name('wanlshop_order o')
                ->join('wanlshop_order_goods g','o.id=g.order_id')
                ->join('wanlshop_shop s','o.shop_id=s.id')
                ->where($where)
                ->field(["sum(g.actual_payment) as price","o.shop_id"])
                ->group("o.shop_id")
                ->buildSql();
                $where['o.state'] =6;
                $sql = Db::name('wanlshop_order_goods')->alias('g')
                    ->join('wanlshop_order o', 'o.id=g.order_id', 'left')
                    ->join('wanlshop_pay_out_trade t', 'o.id=t.order_id', 'left')
                    ->join('pay p', 'p.id=t.pay_way_id', 'left')
                    ->where($where)
                    ->where('p.type','<>','1')
                    //->where('o.createtime', 'between time', [$starttime, $endtime])
                    ->field(['sum(g.number * g.actual_payment ) as price','o.shop_id'])
                    //->where('o.state', '6')
                    ->where('g.gdtype', '0')
                    ->group("o.shop_id")
                    ->buildSql();
                break;
            case '6'://退款金顾

                $where['o.state']=['in',[11]];
                $sql=Db::name('wanlshop_order o')
                    ->join('wanlshop_shop s','o.shop_id=s.id')
                    ->where($where)
                    ->field(["sum(o.actual_price) as price","o.shop_id"])
                    ->group("o.shop_id")
                    ->buildSql();
                break;
        }
        $count=Db::table($sql.'o')->join('wanlshop_shop s','s.id=o.shop_id','right')->where(['s.status'=>'normal'])->count();
        $result = Db::table($sql.'o')->join('wanlshop_shop s','s.id=o.shop_id','right')
            ->order("o.price desc")->field(['s.id','s.shopname','if(o.price,o.price,0) as price'])->where(['s.status'=>'normal'])->page($page,$page_size)
            ->order('o.price desc')->select();

        //echo Db::table($sql.'o')->getLastSql();exit;
        if($result){
            foreach ($result as &$value){
                $value['price'] = sprintf("%.2f",$value['price']);
            }
        }
        $list['list'] = $result ? $result : [];
        $list['total'] = $count ? $count : 0;


        $this->success('ok', $list);
    }



    /**
     * 客户统计
     * 
     * 
     *  @param string $keyword  时间
     *  @param string $name  姓名
     */
    public function userday()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
        $name = $this->request->request('name');

        $where1 = [];
        $where3 = [];
        $where2 = " ";
        if ($keyword) {
            $time = date("Y-m-d", $keyword);

            $time = strtotime($time);
            $endtime = strtotime("+1 day", $keyword);
            $endtime = date("Y-m-d", $endtime);

            $endtime = strtotime($endtime);
            //$where['s.createtime'] = ['between time', [$time, $endtime]];
            $where1['u.createtime'] = ['between time', [$time, $endtime]];
            $where3['u.createtime'] = ['between time', [$time, $endtime]];
            $where2 = " and o.createtime >= " . $time . "
            and o.createtime < " . $endtime;
        }
        if ($name) {
            $where['s.name'] = ['like', '%' . $name . '%'];
        }

        $where['s.shop_id'] = $this->shop_id;
        $where1['s.shop_id'] = $this->shop_id;
        $where3['u.shop_id'] = $this->shop_id;


        $list = Db::name('staff')->alias("s")
            ->where($where)
            ->field('id,name,staffcode,10 as data_1,10 as data_2,10 as data_3,10 as data_4')
            ->order('id desc')
            ->paginate();

        foreach ($list as $k => $row) {
            // $row->shop->visible(['state','shopname']);

            $row['data_1'] = Db::name('user')->alias("u")
                ->join('staff s', 'u.marketing=s.id', 'left')
                ->where($where1)
                ->where('s.id', '=', $row['id'])
                ->count();


            $sql = "
            SELECT COUNT(distinct u.id) as sum
            FROM fa_user as u
            LEFT JOIN fa_wanlshop_order as o on o.user_id = u.id
            WHERE o.state in (2,3,4,5,6)
            and u.marketing = 
                              " . $row['id'] . $where2
            ;
            $data2 = db::query($sql);
            $row['data_2'] = $data2[0]['sum'];

            $sql = "
             SELECT COUNT(distinct u.id) as sum
             FROM fa_user as u
             LEFT JOIN fa_wanlshop_order as o on o.user_id = u.id
             LEFT JOIN fa_wanlshop_order_item as i on o.id = i.order_id
             WHERE o.state in (2,3,4,5,6)
             and i.id is not NULL
             and u.marketing = 
             " . $row['id'] . $where2
            ;
            $data3 = db::query($sql);
            $row['data_3'] = $data3[0]['sum'];

            $sql = "
              SELECT COUNT(distinct u.id) as sum
              FROM fa_user as u
              LEFT JOIN fa_wanlshop_order as o on o.user_id = u.id
              LEFT JOIN fa_wanlshop_order_item as i on o.id = i.order_id
              WHERE o.state in (2,3,4,5,6)
              and i.type = 4
              and u.marketing = 
              " . $row['id'] . $where2
            ;
            $data4 = db::query($sql);
            $row['data_4'] = $data4[0]['sum'];


            $list[$k] = $row;
        }

        // $list['data_1'] = Db::name('user')->alias("u")
        //     //->join('staff s', 'u.marketing=s.id', 'left')
        //     ->where($where3)
        //     ->count();
        // ;
        // // 新增会员


        // $sql = "
        // SELECT COUNT(distinct u.id) as sum
        // FROM fa_user as u
        // LEFT JOIN fa_wanlshop_order as o on o.user_id = u.id
        // WHERE o.state in (2,3,4,5,6)
        //                   " . $where2
        // ;
        // $data5 = db::query($sql);

        // $list['data_2'] = $data5[0]['sum'];
        // // 成交会员

        // $sql = "
        // SELECT COUNT(distinct u.id) as sum
        // FROM fa_user as u
        // LEFT JOIN fa_wanlshop_order as o on o.user_id = u.id
        // LEFT JOIN fa_wanlshop_order_item as i on o.id = i.order_id
        // WHERE o.state in (2,3,4,5,6)
        // and i.id is not NULL
         
        // " . $where2
        // ;
        // $data6 = db::query($sql);

        // $list['data_3'] = $data6[0]['sum'];
        // // 开卡会员

        // $sql = "
        // SELECT COUNT(distinct u.id) as sum
        // FROM fa_user as u
        // LEFT JOIN fa_wanlshop_order as o on o.user_id = u.id
        // LEFT JOIN fa_wanlshop_order_item as i on o.id = i.order_id
        // WHERE o.state in (2,3,4,5,6)
        // and i.type = 4
        
        // " . $where2
        // ;
        // $data7 = db::query($sql);
        // $list['data_4'] = $data7[0]['sum'];
        // // 充值客户


        $this->success('ok', $list);

    }

    /**
     * 业绩统计
     * 
     * 
     *  @param string $keyword  时间
     *  @param string $name  姓名
     */
    public function performanceday()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
        $name = $this->request->request('name');

        $where = [];
        $where1 = [];
        if ($keyword) {
            $time = date("Y-m-d", $keyword);

            $time = strtotime($time);
            $endtime = strtotime("+1 day", $keyword);
            $endtime = date("Y-m-d", $endtime);

            $endtime = strtotime($endtime);
            $where1['o.createtime'] = ['between time', [$time, $endtime]];
        }
        if ($name) {
            $where['s.name'] = ['like', '%' . $name . '%'];
        }
        if($this->shop_id !=1){
            $where['shop_id'] = $this->shop_id;
        }
        $where1['o.shop_id'] = $this->shop_id;

        $list = Db::name('staff')->alias('s')
           // ->join('wanlshop_order o','1=1','left')
            ->where($where)
            ->field('s.id,name,staffcode,0 as data_1,0 as data_2')
            ->order('s.id desc')
            ->paginate();
        foreach ($list as $k => $row) {
            $row['data_1'] = Db::name('wanlshop_order')->alias('o')
                ->where($where1)
                ->where('o.staff_ids', '=', $row['id'])
                ->count();

            $data2 = Db::name('wanlshop_order')->alias('o')
                ->field('sum(o.actual_price) as sum')
                ->where($where1)
                ->where('o.staff_ids', '=', $row['id'])
                ->select();
            $row['data_2'] = $data2[0]['sum'] ? $data2[0]['sum'] : "0.00";


            $list[$k] = $row;
        }
        // $list['data_1'] = Db::name('wanlshop_order')->alias('o')
        //     ->where($where1)
        //     ->count();
        // ;
        // 开单

        // $data4 = Db::name('wanlshop_order')->alias('o')
        //     ->field('sum(o.actual_price) as sum')
        //     ->where($where1)
        //     ->select();

        // $list['data_2'] = $data4[0]['sum'] ? $data4[0]['sum'] : "0.00"; // 业绩

        $this->success('ok', $list);

    }

    /**
     * 服务统计
     * 
     * 
     *  @param string $keyword  时间
     *  @param string $name  姓名
     */
    public function serviceday()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
        $name = $this->request->request('name');

        $where = [];
        $where1 = [];
        if ($keyword) {
            $time = date("Y-m-d", $keyword);

            $time = strtotime($time);
            $endtime = strtotime("+1 day", $keyword);
            $endtime = date("Y-m-d", $endtime);

            $endtime = strtotime($endtime);
            $where1['o.createtime'] = ['between time', [$time, $endtime]];
        }
        if ($name) {
            $where['name'] = ['like', '%' . $name . '%'];
        }

        $where1['shop_id'] = $this->shop_id;
        $where1['gdtype'] = 1;

        $list = Db::name('staff')
            ->where($where)
            ->field('id,name,staffcode,0 as data_1,0.00 as data_2,0.00 as data_3')
            ->order('id desc')
            ->paginate();

        foreach ($list as $k => $row) {
            $data1 = Db::name('wanlshop_order_goods')->alias('o')
                ->field('sum(o.number) as sum')
                ->where($where1)
                ->where('o.staff_id', '=', $row['id'])
                ->select();
            $row['data_1'] = $data1[0]['sum'] ? $data1[0]['sum'] : "0.00";

            $data2 = Db::name('wanlshop_order_goods')->alias('o')
                ->field('sum(actual_payment) as sum,sum(o.price) as persum')
                ->where($where1)
                ->where('o.staff_id', '=', $row['id'])
                ->select();
            $row['data_2'] = $data2[0]['sum'] ? $data2[0]['sum'] : "0.00";
            $row['data_3'] = $data2[0]['persum'] ? $data2[0]['persum'] : "0.00";

            $list[$k] = $row;
        }


        // $data4 = Db::name('wanlshop_order_goods')->alias('o')
        //     ->field('sum(o.number) as sum')
        //     ->where($where1)
        //     //->where('o.staff_id','=',$row['id'])
        //     ->select();



        // $list['data_1'] = $data4[0]['sum'] ? $data4[0]['sum'] : "0.00";
        // ;
        // // 次数

        // $data5 = Db::name('wanlshop_order_goods')->alias('o')
        //     ->field('sum(actual_payment) as sum,sum(o.price) as persum')
        //     ->where($where1)
        //     // ->where('o.staff_id','=',$row['id'])
        //     ->select();
        // $list['data_2'] = $data5[0]['sum'] ? $data5[0]['sum'] : "0.00";
        // // 实收

        // $list['data_3'] = $data5[0]['persum'] ? $data5[0]['persum'] : "0.00";
        // // 客户耗金



        $this->success('ok', $list);

    }

    /**
     * 销售统计
     * 
     * 
     *  @param string $keyword  时间
     *  @param string $name  姓名
     */
    public function saleday()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
        $name = $this->request->request('name');

        $where = [];
        $where1 = [];
        if ($keyword) {
            $time = date("Y-m-d", $keyword);

            $time = strtotime($time);
            $endtime = strtotime("+1 day", $keyword);
            $endtime = date("Y-m-d", $endtime);

            $endtime = strtotime($endtime);
            $where1['createtime'] = ['between time', [$time, $endtime]];
        }
        if ($name) {
            $where['name'] = ['like', '%' . $name . '%'];
        }
        $where1['shop_id'] = $this->shop_id;


        $list = Db::name('staff')
            ->where($where)
            ->field('id,name,staffcode,10 as data_1,1000.00 as data_2,5000.00 as data_3')
            ->order('id desc')
            ->paginate();

        foreach ($list as $k => $row) {
            $row['data_1'] = Db::name('wanlshop_order')->alias('o')
                ->field('sum(o.number) as sum')
                ->where($where1)
                ->where('o.staff_id', '=', $row['id'])
                ->count();

            $data2 = Db::name('wanlshop_order')->alias('o')
                ->field('sum(actual_price) as sum,sum(o.price) as persum')
                ->where($where1)
                ->where('o.staff_id', '=', $row['id'])
                ->select();
            $row['data_2'] = $data2[0]['sum'] ? $data2[0]['sum'] : "0.00";
            $row['data_3'] = "0.00";

            $list[$k] = $row;
        }


        // $list['data_1'] = Db::name('wanlshop_order')->alias('o')
        //     ->field('sum(o.number) as sum')
        //     ->where($where1)
        //     // ->where('o.staff_id','=',$row['id'])
        //     ->count(); // 次数


        // $data4 = Db::name('wanlshop_order')->alias('o')
        //     ->field('sum(actual_price) as sum,sum(o.price) as persum')
        //     ->where($where1)
        //     //->where('o.staff_id','=',$row['id'])
        //     ->select();


        // $list['data_2'] = $data4[0]['sum'] ? $data4[0]['sum'] : "0.00"; // 实收
        // $list['data_3'] = "0.00"; // 客户耗金



        $this->success('ok', $list);

    }


    /**
     * 员工统计
     * 
     * 
     *  @param string $keyword  时间
     *  
     */
    public function staffday()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
        $name = $this->request->request('name');

        $where2 = " ";
        if ($keyword) {
            $time = date("Y-m-d", $keyword);

            $time = strtotime($time);
            $endtime = strtotime("+1 day", $keyword);
            $endtime = date("Y-m-d", $endtime);

            $endtime = strtotime($endtime);
            $where['createtime'] = ['between time', [$time, $endtime]];
            $where2 = " and o.createtime >= " . $time . "
            and o.createtime < " . $endtime;
        }

        if ($this->shop_id != 1) {
            $where['shop_id'] = $this->shop_id;
        }
        //$where['shop_id'] = $this->shop_id;

        $list['data_11'] = Db::name('user')->alias("u")
            //->join('staff s', 'u.marketing=s.id', 'left')
            ->where($where)
            ->count();
        //echo Db::name('user')->getLastSql();exit;
        // 新增会员


        $sql = "
        SELECT COUNT(distinct u.id) as sum
        FROM fa_user as u
        LEFT JOIN fa_wanlshop_order as o on o.user_id = u.id
        WHERE o.state in (2,3,4,5,6)
                          " . $where2
        ;
        $data5 = db::query($sql);

        $list['data_12'] = $data5[0]['sum'];
        // 成交会员

        $sql = "
        SELECT COUNT(distinct u.id) as sum
        FROM fa_user as u
        LEFT JOIN fa_wanlshop_order as o on o.user_id = u.id
        LEFT JOIN fa_wanlshop_order_item as i on o.id = i.order_id
        WHERE o.state in (6) and o.data_type !='merchant' and i.type in(1,2,3)
        and i.id is not NULL
         
        " . $where2;

        $data6 = db::query($sql);

        $list['data_13'] = $data6[0]['sum'];
        // 开卡会员

        $sql = "
        SELECT COUNT(distinct u.id) as sum
        FROM fa_user as u
        LEFT JOIN fa_wanlshop_order as o on o.user_id = u.id
        LEFT JOIN fa_wanlshop_order_item as i on o.id = i.order_id
        WHERE o.state in (6) and o.data_type !='merchant' and i.type in(4)
        and i.id is not NULL  
        " . $where2;
        $data7 = db::query($sql);
        $list['data_14'] = $data7[0]['sum'];
        // 充值客户




        $list['data_21'] = Db::name('wanlshop_order')->alias('o')
            ->where($where)
            ->where(['state'=>['in',[6]]])
            ->count();
        ;
        // 开单

        $data4 = Db::name('wanlshop_order')->alias('o')
            ->field('sum(o.actual_price) as sum')
            ->where($where)
            ->where(['state'=>['in',[6]]])
            ->select();

        $list['data_22'] = $data4[0]['sum'] ? $data4[0]['sum'] : "0.00"; // 业绩





        $data33 = Db::name('wanlshop_order_goods')->alias('o')
            ->field('sum(o.number) as sum')
            ->where($where)
            //->where('o.staff_id','=',$row['id'])
            ->select();



        $list['data_31'] = $data33[0]['sum'] ? $data33[0]['sum'] : "0.00";
        ;
        // 次数

        $data35 = Db::name('wanlshop_order_goods')->alias('o')
            ->field('sum(actual_payment) as sum,sum(o.price) as persum')
            ->where($where)
            // ->where('o.staff_id','=',$row['id'])
            ->select();
        $list['data_32'] = $data35[0]['sum'] ? $data35[0]['sum'] : "0.00";
        // 实收

        $list['data_33'] = $data35[0]['persum'] ? $data35[0]['persum'] : "0.00";
        // 客户耗金




        $list['data_41'] = Db::name('wanlshop_order')->alias('o')
            ->field('sum(o.number) as sum')
            ->where($where)
            // ->where('o.staff_id','=',$row['id'])
            ->count(); // 次数


        $data44 = Db::name('wanlshop_order')->alias('o')
            ->field('sum(actual_price) as sum,sum(o.price) as persum')
            ->where($where)
            //->where('o.staff_id','=',$row['id'])
            ->select();


        $list['data_42'] = $data44[0]['sum'] ? $data44[0]['sum'] : "0.00"; // 实收
        $list['data_43'] = "0.00"; // 客户耗金



        $this->success('ok', $list);

    }








    /**
     * 订单详情 -商品/服务
     * 
     * @param int $id  订单id
     *  
     */
    public function orderinfo()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $order_id = $this->request->request('id');
        // $time = date("Y-m", $keyword);

        // $time =  strtotime($time);
        // $endtime = strtotime("+1 month", $keyword);
        // $endtime = date("Y-m", $endtime);

        // $endtime = strtotime($endtime);

        //  if ($keyword) {
        //      $where['clocktime'] = ['between time', [$time, $endtime]];      
        //  }

        $where['o.id'] = $order_id;
        // $where['s.gdtype'] = 0;

        $list = Db::name('wanlshop_order o')
            ->join('wanlshop_order_goods s', 's.order_id = o.id', 'inner')
            ->join('user u', 'u.id = o.user_id', 'left')
            ->join('wanlshop_shop p', 'p.id = o.shop_id', 'left')
            ->where($where)
            ->field('o.*,s.*,u.username,u.nickname,u.avatar')
            ->order('s.id desc')
            ->paginate();


        $this->success('ok', $list);

    }

    /**
     * 订单列表 -卡项
     * 
     * @param string $state
     *  
     */
    public function itemlist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $state = $this->request->request('state');
        // $keyword = $this->request->request('keyword'); 
        // $time = date("Y-m", $keyword);

        // $time =  strtotime($time);
        // $endtime = strtotime("+1 month", $keyword);
        // $endtime = date("Y-m", $endtime);

        // $endtime = strtotime($endtime);

        //  if ($keyword) {
        //      $where['clocktime'] = ['between time', [$time, $endtime]];      
        //  }

        $where['o.shop_id'] = $this->shop_id;
        // $where['s.gdtype'] = 0;
        if ($state) {
            $where['o.state'] = $state;
        }

        $list = Db::name('wanlshop_order o')
            ->join('wanlshop_order_item s', 's.order_id = o.id', 'inner')
            ->where($where)
            ->field('o.id,s.name,s.image,type,s.actual_price,o.order_no,o.actual_price,o.state')
            ->order('s.id desc')
            ->paginate();

        $this->success('ok', $list);

    }

    /**
     * 订单详情 -卡项
     * 
     * @param int $id  订单id
     *  
     */
    public function iteminfo()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $order_id = $this->request->request('id');
        // $time = date("Y-m", $keyword);

        // $time =  strtotime($time);
        // $endtime = strtotime("+1 month", $keyword);
        // $endtime = date("Y-m", $endtime);

        // $endtime = strtotime($endtime);

        //  if ($keyword) {
        //      $where['clocktime'] = ['between time', [$time, $endtime]];      
        //  }

        $where['o.id'] = $order_id;
        // $where['s.gdtype'] = 0;

        $list = Db::name('wanlshop_order o')
            ->join('wanlshop_order_item s', 's.order_id = o.id', 'inner')
            ->join('wanlshop_order_item_info i', 'i.order_item_id = s.id', 'left')
            ->join('user u', 'u.id = o.user_id', 'left')
            ->join('wanlshop_shop p', 'p.id = o.shop_id', 'left')
            ->where($where)
            ->field('o.*,s.*,u.username,u.nickname,u.avatar')
            ->order('s.id desc')
            ->paginate();

        $this->success('ok', $list);

    }

    /**
     * 预约列表
     * 
     *  @param string $keyword  姓名\编号\手机
     *  @param string $status   状态
     *  
     */
    public function reslist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
        $status = $this->request->request('status');


        $where['r.shop_id'] = $this->shop_id;
        // $where['user_id'] = $this->auth->id;


        if ($keyword) {
            $where['r.name|r.mobile'] = ['like', '%' . $keyword . '%'];
        }


        if ($status) {
            $where['r.status'] = $status;
        }

        $list = Db::name('reservation r')
            ->join('user u', 'u.id = r.user_id', 'left')
            ->where($where)
            ->field('r.*,u.nickname,u.avatar')
            ->order('r.updatetime desc')
            ->paginate();

        $this->success('ok', $list);


    }

    /**
     * 预约状态改变
     * 
     *  @param string $res_id  预约id
     *  @param string $status   状态
     *  
     */
    public function resedit()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $res_id = $this->request->request('res_id');
        $status = $this->request->request('status');


        //$where['r.shop_id'] = $this->shop_id;
        //$where['user_id'] = $this->auth->id;
        $params = [
            'status' => $status,
            'updatetime' => time()

        ];

        $where['id'] = $res_id;

        $list = Db::name('reservation')
            ->where($where)
            ->update($params);

        $this->success('ok', $list);


    }

    /**
     * 新增客户
     * 
     * 
     *  @param string $username  姓名
     *  @param string $nickname  备注名
     *  @param string $mobile   手机号
     *  @param string $code   客户编号
     *  @param string $juser   客户来源
     *  @param int $shop   门店
     *  @param int $gender   男女
     *  @param int $birthday   生日
     *  @param string $wx   微信
     *  @param string $city   地址
     *  @param string $adds   详细地址
     *  @param string $remark   备注
     *  
     */
    public function useradd()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $username = $this->request->request('username');
        $nickname = $this->request->request('nickname');
        $mobile = $this->request->request('mobile');
        $code = $this->request->request('code');
        $juser = $this->request->request('juser');
        $shop = $this->request->request('shop');

        $gender = $this->request->request('gender');
        $birthday = $this->request->request('birthday');
        $wx = $this->request->request('wx');
        $city = $this->request->request('city');
        $adds = $this->request->request('adds');
        $remark = $this->request->request('remark');

        $date = [
            'group_id' => 1,
            'username' => $username,
            'nickname' => $nickname,
            'mobile' => $mobile,
            'code' => $code,
            'juser_id' => $juser,
            'shop_id' => $shop,
            'gender' => $gender,
            'birthday' => $birthday,
            'remark' => $remark,
            'wechat' => $wx,
            'city' => $city,
            'address' => $adds,
            'jointime' => time(),
            'createtime' => time()

        ];

        $list = Db::name('user')->insert($date);

        $this->success('ok', $list);

    }




    /**
     * 新增预约
     * 
     * 
     *  @param int $stime  时间
     *  @param string $name  姓名
     *  @param string $mobile  手机号
     */
    public function resadd()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $stime = $this->request->request('stime');
        $name = $this->request->request('name');
        $mobile = $this->request->request('mobile');

        $date = [
            'name' => $name,
            'stime' => $stime,
            'mobile' => $mobile,
            'shop_id' => $this->shop_id,
            'createtime' => time()

        ];

        $list = Db::name('reservation')->insert($date);

        $this->success('ok', $list);

    }

    /**
     * 库存盘点
     * 
     *  @param string $keyword  名称\编号
     *  @param string $status   状态1商品2耗材3预警
     */
    public function gooslist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $keyword = $this->request->request('keyword');
        $status = $this->request->request('status');

        if ($keyword) {
            $where['o.title|o.code'] = ['like', '%' . $keyword . '%'];
        }

        $where['o.status'] = 'normal';
        $where['o.shop_id'] = $this->shop_id;
        // $where['s.gdtype'] = 0;

        $list = Db::name('wanlshop_goods o')
            //->join('wanlshop_goods_sku s', 's.goods_id = o.id', 'left')
            ->where($where)
            //->field('o.*,s.*,u.username,u.nickname,u.avatar')
            ->order('o.id desc')
            ->paginate();

        foreach ($list as $k => $row) {
            // $row->shop->visible(['state','shopname']);
            $row['info'] = Db::name('wanlshop_goods_sku')
                ->where('goods_id', '=', $row['id'])
                //->field('id,title,number,actual_payment')
                ->select();
            $list[$k] = $row;

        }

        $this->success('ok', $list);

    }

    /**
     * 考核列表
     * 
     * @param string $status   状态0=进行中,1=已完成,2=已停止
     *  
     */
    public function examinelist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        // $keyword = $this->request->request('keyword'); 
        $status = $this->request->request('status');

        if(!empty($status)) {
            $where['e.status'] = $status;  
           
        }

        $id = $this->shop_id;
        $where[] = ['exp', Db::raw("FIND_IN_SET($id,e.shop_ids)")];

        // $where['status'] = 'normal';
        // $where['o.id'] =  $this->shop_id;
        // $where['s.gdtype'] = 0;

        $list = Db::name('examine e')
            ->join('examine_shop s', 's.examine_id = e.id and s.shop_id = ' . $id, 'left')
            ->where($where)
            ->field('e.*,s.score,s.status as status_1,case when e.ok_score <= s.score then "合格" else "不合格" end as status_ok ')
            ->order('e.id desc')
            ->paginate();

        $this->success('ok', $list);

    }

    /**
     * 新增考核
     * 
     * 
     *  @param int $examine_id  考核ID
     *  @param string $score  分数
     *  @param string $remarks  备注
     */
    public function examineadd()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $examine_id = $this->request->request('examine_id');
        $score = $this->request->request('score');
        $remarks = $this->request->request('remarks');

        $date = [
            'examine_id' => $examine_id,
            'score' => $score,
            'remarks' => $remarks,
            'shop_id' => $this->shop_id,
            'createtime' => time(),
            'create_id' => $this->auth->staff_id

        ];

        $list = Db::name('examine_shop')->insert($date);

        $this->success('ok', $list);

    }


    /**
     * 生命周期列表
     * 
     * 
     *  
     */
    public function shopauth()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        //$shop_id = $this->request->request('shop'); 

        $where['s.id'] = $this->shop_id;

        $list1 = Db::name('wanlshop_shop s')
            ->join('wanlshop_auth a', 'a.id = s.auth_id', 'left')
            ->field('a.id,a.verify,a.createtime,a.updatetime')
            ->where($where)
            ->find();
        $list2 = Db::name('wanlshop_shop s')
            ->join('wanlshop_auth_accredit a', 'a.wanlshop_auth_id = s.auth_id', 'left')
            ->field('a.verify,a.createtime,a.examinetime')
            ->where($where)
            ->find();
        $list3 = Db::name('wanlshop_shop s')
            ->join('wanlshop_auth_activity a', 'a.wanlshop_auth_id = s.auth_id', 'left')
            ->field('a.verify,a.createtime,a.examinetime')
            ->where($where)
            ->find();
        $list4 = Db::name('wanlshop_shop s')
            ->join('wanlshop_auth_business a', 'a.wanlshop_auth_id = s.auth_id', 'left')
            ->field('a.verify,a.createtime,a.examinetime')
            ->where($where)
            ->find();
        $list5 = Db::name('wanlshop_shop s')
            ->join('wanlshop_auth_design a', 'a.wanlshop_auth_id = s.auth_id', 'left')
            ->field('a.verify,a.createtime,a.examinetime,a.id')
            ->where($where)
            ->find();
        $list6 = Db::name('wanlshop_shop s')
            ->join('wanlshop_auth_financial a', 'a.wanlshop_auth_id = s.auth_id', 'left')
            ->field('a.verify,a.createtime,a.examinetime')
            ->where($where)
            ->find();
        $list7 = Db::name('wanlshop_shop s')
            ->join('wanlshop_auth_selection a', 'a.wanlshop_auth_id = s.auth_id', 'left')
            ->field('a.verify,a.createtime,a.examinetime,a.id')
            ->where($where)
            ->find();
        $list8 = Db::name('wanlshop_shop s')
            ->join('wanlshop_auth_supply a', 'a.wanlshop_auth_id = s.auth_id', 'left')
            ->field('a.verify,a.createtime,a.examinetime')
            ->where($where)
            ->find();
        $list9 = Db::name('wanlshop_shop s')
            ->join('wanlshop_auth_train a', 'a.wanlshop_auth_id = s.auth_id', 'left')
            ->field('a.verify,a.createtime,a.examinetime')
            ->where($where)
            ->find();
        $list10 = Db::name('wanlshop_shop s')
            ->join('wanlshop_auth_goods a', 'a.wanlshop_auth_id = s.auth_id', 'left')
            ->field('a.verify,a.createtime,a.examinetime')
            ->where($where)
            ->find();



        $date['auth'] = $list1;
        $date['accredit'] = $list2;
        $date['activity'] = $list3;
        $date['business'] = $list4;
        $date['design'] = $list5;
        $date['financial'] = $list6;
        $date['selection'] = $list7;
        $date['supply'] = $list8;
        $date['train'] = $list9;
        $date['goods'] = $list10;


        $this->success('ok', $date);

    }

    //
    /**
     * 选址评估
     * 
     * 
     *  @param int $id  
     *  @param string $address  地址
     *  @param string $addimages  周边图片
     *  @param string $addfile  周边视频
     *  @param string $name  联系人
     *  @param string $moblie  联系电话
     *  @param string $shopremark  备注
     */
    public function selectionedit()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $id = $this->request->request('id');
        $addimages = $this->request->request('addimages');
        $address = $this->request->request('address');
        $addfile = $this->request->request('addfile');
       
        $date = [
            'id' => $id,
            'addimages' => $addimages,
            'address' => $address,
            'addfile' => $addfile,
            'verify'   =>1,
            //'createtime' => time(),
            //'create_id' => $this->auth->staff_id

        ];

      
 

        $list = Db::name('wanlshop_auth_selection')->update($date);

        $this->success('ok', $list);

    }

    /**
     * 装修设计
     * 
     * 
     *  @param int $id  
     *  @param string $doorimages  门头
     *  @param string $interiorimages  内景
     *  @param string $shopfile  视频
     *  @param string $shopremark  装修备注
     *  @param string $checkstandimages  收银台
     */
    public function wanlshop_auth_design()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $id = $this->request->request('id');
        $doorimages = $this->request->request('doorimages');
        $interiorimages = $this->request->request('interiorimages');
        $checkstandimages = $this->request->request('checkstandimages');
        $shopremark = $this->request->request('shopremark');
        $shopfile = $this->request->request('shopfile');
       
        $date = [
            'id' => $id,
            'doorimages' => $doorimages,
            'interiorimages' => $interiorimages,
            'checkstandimages' => $checkstandimages,
            'shopremark' => $shopremark,
            'shopfile' => $shopfile,
            'verify'   =>1,
            //'createtime' => time(),
            //'create_id' => $this->auth->staff_id

        ];

      


        $list = Db::name('wanlshop_auth_design')->update($date);

        $this->success('ok', $list);

    }

}
